<template>
  <div class="app-container">
    <!-- 添加或修改微信支付对话框 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="应用ID" prop="appId">
        <el-input v-model="form.appId" placeholder="请输入应用编号"/>
      </el-form-item>
      <el-form-item label="商户号" prop="mchId">
        <el-input v-model="form.mchId" placeholder="请输入商户号"/>
      </el-form-item>
      <el-form-item label="秘钥" prop="apiKey">
        <el-input v-model="form.apiKey" placeholder="请输入秘钥"/>
      </el-form-item>
      <el-form-item label="通知回调" prop="notifyUrl">
        <el-input v-model="form.notifyUrl" placeholder="请输入通知回调"/>
      </el-form-item>
      <el-form-item label="退款回调" prop="refundNotifyUrl">
        <el-input v-model="form.refundNotifyUrl" placeholder="请输入退款回调"/>
      </el-form-item>
      <el-form-item label="证书" prop="certPath">
        <file-upload :limit="1" :is-show-tip="false" v-model="form.certPath"/>
      </el-form-item>
    </el-form>
    <div style="display: flex;justify-content: center" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>

<script>
import {

  addWechatPay, allWechatPays,
  updateWechatPay
} from "@/api/system/wechat_pay";

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      allWechatPays().then(re => {
        if (re.data.length > 0) {
          this.form = re.data[0]
        } else {
          this.form = {}
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWechatPay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getInfo();
            });
          } else {
            addWechatPay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getInfo();
            });
          }
        }
      });
    },
  }
};
</script>
